package cn.itcast.bos.utils;

import cn.itcast.bos.utils.MailUtils;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件的工具类:
 * @author admin
 *
 */
public class LocalHostMailUtils {

    public static void sendMail(String to,String activecode,String telephone){

        try {
            // 获得连接:
            Properties props = new Properties();
            Session session = Session.getInstance(props, new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("service@bos.com", "password");
                }

            });
            // 构建邮件:
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("service@bos.com"));
            // 设置收件人:
            // TO:收件人   CC:抄送   BCC:暗送,密送.
            message.addRecipient(RecipientType.TO, new InternetAddress(to));
            // 主题:
            message.setSubject("来自速运快递的激活邮件!");
            // 正文:
            message.setContent("尊敬的用户,请于24之内完成邮箱绑定,请点击下面的链接:</br><a href='http://localhost:8082/customer_activeMail?telephone="+telephone+"&activeCode="+activecode+"'>速运快递邮箱绑定地址</a>","text/html;charset=UTF-8");

            // 发送邮件:
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
